
<?php
class upload_image {
    var $upload_file_name;
    var $upload_file_size;
    var $upload_max_size = 20971520; // max_size=20M
    var $upload__file_dir = "uploadfile/";
    var $upload_tamp_dir;
    var $upload_file_type;
    
    var $errorstr;
    
    function get_upload_name($upload_file_name){
         $type = end(explode('.',$upload_file_name));
         return $this->upload_file_name=$this->upload__file_dir.md5(date("ymdhis")).'.'.$type;
    }
    
    function get_upload_type($upload_file_type){
        return $this->upload_file_type = $upload_file_type;
    }
   
    function get_upload_size($upload_file_size){
        return $this ->upload_file_size = $upload_file_size;
    }
    
    function get_upload_tamp($upload_tamp_dir){
        return $this->upload_tamp_dir = $upload_tamp_dir;
    }
   
    function check_upload_dir(){
        if(!file_exists($this ->upload__file_dir)){
              mkdir($this->upload__file_dir);
        }
    }
    
    function check_upload_size(){
        if($this ->upload_file_size > $this->upload_max_size){
           $this->halt("Upload file is too big");
           exit;
         }
    }

    function uplod_image() {
        if(!move_uploaded_file($this->upload_tamp_dir,$this->upload_file_name)){
            $this->halt("Upload failed");
            exit;
        }else{
            $this->halt("Upload success");
        }

    }
    
    function halt($errorstr){
        echo '<script>alert("'.$errorstr.'");history.go(-1);</script>';
    }
   
    function upload(){
      
        $this->check_upload_size();
        $this->check_upload_dir();
        $this->uplod_image();
    }
}
?>
